clear;close all;clc;

% we read in our single-source recordings to create our own models at 4
% frequencies.

rad=0.2;
Norder=4;
tdur = 40e-3;

thre = 0.2;
% freqs= [300 600 1500 3000]; %just a random frequency
% freqs= [300 500 600 850 1000 1200  1500 2000 3000]; %just a random frequency
% nturns=[0.1 0.2 0.5 0.97 1.11 1.5 1.8 2 2.5 2.8 3 3.8];
% freqs= [650 700 800 850 1000]; %just a random frequency
% nturns = [0.8 0.9 0.95 0.97 1.11];
freqs= [850 1000]; %just a random frequency
nturns = [0.97 1.11];
fs = 44100;
% whos
plot_each =1;

projection1 = 0; % neariest center
Num_centers = 1;
% Proto_Num = 1.2;
Proto_Num = 5.2;
addpath('C:\Users\gaiy\OneDrive - Saint Louis University\speech_experiments\Analyze_localization_result\Protocols_related');
out_table=f_P5_2_table;

Angles = 1:20;
sides = {'Left','Right'};

countf = 0;
for ifreql = 1:length(freqs)
    % for ifreql = 1
    f1=figure('position',[100 70 600 700]);
    freq = freqs(ifreql)
    for iangle = 1:length(Angles)
        for ichunk = 1:3
            figure(f1)
            countf=countf+1;
            subplot(4,3,countf);
            % cc = 0;
            Angle = Angles(iangle);
            PString = num2str(Proto_Num);
            %         if Angle < 0
            eval(['load All_sound_recordings\Protocol_' PString(1) '\Protocol_' PString '_' sides{1} '_'   num2str(abs(Angles(iangle))) 'degree.mat']);
            %         elseif Angle ==0
            %             eval(['load All_sound_recordings\Protocol_' PString(1) '\Protocol_' PString '_'  num2str(Angles(iangle)) 'degree.mat']);
            %         else
            %             eval(['load All_sound_recordings\Protocol_' PString(1) '\Protocol_' PString '_' sides{2} '_'   num2str(Angles(iangle)) 'degree.mat']);
            %         end

            NNN = round(length(recorded_sound)./3);
            begin_P = (ichunk-1).*NNN+1;
            end_P = ichunk.*NNN;
            left_sound = recorded_sound(begin_P:end_P,1);
            right_sound = recorded_sound(begin_P:end_P,2);
            [SL,SR,freq_pos,flag]=mySTFT(left_sound,right_sound,fs,thre,tdur);
            % normalization:
            % [SL1,SR1]=Normalize(SL,SR);
            % if Angle < 0
            %     L_or_R=1;
            % else
            L_or_R=2;
            % end
            SLR=Normalize_Both(SL,SR,L_or_R);
            [Y Ind] = min(abs(freq_pos - freq));
            X_P = [real(SLR(Ind,flag>0))' imag(SLR(Ind,flag>0))'];
            plot(X_P(:,1),X_P(:,2),'k.');hold on;axis square
            % end

            out = f_mycenter(X_P,rad);
            F_centers(iangle,:) = out.C1;

            s=plot(F_centers(iangle,1),F_centers(iangle,2),'go');hold on;set(s,'linewidth',2)
            if ichunk==3
                s=plot(out.C2(1),out.C2(2),'mo');hold on;set(s,'linewidth',2)
            end
            %         grid on
            set(gca,'Xtick',-1:0.5:1)
            set(gca,'Ytick',-1:0.5:1)
            if Angle < 0
                Pangle =['-' num2str(abs(Angles(iangle))) ];
            elseif Angle ==0
                Pangle =[num2str(abs(Angles(iangle))) ];
            else
                Pangle =['+' num2str(abs(Angles(iangle))) ];
            end
            %         title([num2str(freq) 'Hz;' Pangle]);
            title([num2str(freq) 'Hz;' 'True:' num2str(out_table(iangle,2)) ' & ' num2str(out_table(iangle,3)) ]);
            axis([-1 1 -1 1])

         
        end
    end % angle


end % freq



